<script setup lang="ts">
import { ref } from 'vue'

import { Page, useVbenModal } from '@vben/common-ui'

import { Button, Table } from 'ant-design-vue'

import { getRevicedList } from '#/api/spuBom'

import Split from './Split.vue'

const emit = defineEmits(['refresh'])

const PurchaseList = ref([])

const columns: any = [
  { title: '收料单号', dataIndex: 'record_sn', align: 'center' },
  { title: '收料日期', dataIndex: 'record_date', align: 'center' },
  { title: '收料数量', dataIndex: 'action_num', align: 'center' },
  { title: '收料供应商', dataIndex: 'SupplyNa', align: 'center' },
  { title: '收料单价', dataIndex: 'price', align: 'center' },
  { title: '收料仓库', dataIndex: 'warehouse_name', align: 'center' },
  { title: '采购单号', dataIndex: 'caigou_id', align: 'center' },
  { title: '手工单号', dataIndex: 'purchase_sn', align: 'center' },
  { title: '面料名称', dataIndex: 'material_name', align: 'center' },
  { title: '操作', dataIndex: 'action', align: 'center' },
]
const purchase_bom_detail_id = ref("")
const [Modal, modalApi] = useVbenModal({
  class: 'w-2/3',
  showConfirmButton: false,
  onCancel() {
    modalApi.close()
  },
  async onConfirm() {},
  onOpenChange: async (isOpen: boolean) => {
    if (!isOpen) return
    const { data } = modalApi.getData<Record<string, any>>()
    purchase_bom_detail_id.value = data.purchase_bom_detail_id
    initdata()
  },
})

function  initdata(){
  getRevicedList({ purchase_bom_detail_id: purchase_bom_detail_id.value }).then((res) => {
    PurchaseList.value = res.data.data
  })
}

const [SplitModel, SplitApi] = useVbenModal({
  connectedComponent: Split,
})
function openSplit(data: any) {
  SplitApi.setData({ data }).open()
}
function splitRefresh() {
  initdata()
  emit('refresh')
}
</script>

<template>
  <Modal title="拆单-收料记录">
    <Page>
      <SplitModel @refresh="splitRefresh" />
      <Table class="w-fill" bordered :data-source="PurchaseList" :columns="columns">
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'res'">{{ record.material_name }}-{{ record.color_name }}-{{ record.SupplyNa }}</template>
          <template v-if="column.dataIndex === 'action'">
            <Button type="primary" @click="openSplit(record)" size="small">备料拆单</Button>
          </template>
        </template>
      </Table>
    </Page>
  </Modal>
</template>

<style scoped></style>
